//
//  HNPickView.m
//  BestLearning
//
//  Created by 星道 on 17/5/16.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HNPickView.h"
#import "HNRegion.h"
@implementation HNPickView
{
    
    IBOutlet UIPickerView *_PickView;
    HNRegion *_Model;
    NSInteger _ProviceIndex;
    NSInteger _CityIndex;
    NSInteger _AreaIndex;
    NSInteger _LeveIndex;
}

- (void)Config
{
    _PickView.delegate = self;
    _PickView.dataSource = self;
}


- (void)Show:(HNRegion *)Model RegionType:(RegionType)RegionType
{
    _Model = Model;
    _ProviceIndex = 0;
    _CityIndex = 0;
    _AreaIndex = 0;
    
    self.hidden = NO;
    [_PickView selectRow:0 inComponent:0 animated:NO];
    self.RegionType = RegionType;
    [_PickView reloadAllComponents];
    [UIView animateWithDuration:0.3 animations:^{
       
        self.frame = CGRectMake(0, SCREENH_HEIGHT-250, SCREEN_WIDTH, 250);
    }];
    
    
}

- (void)Close
{
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, SCREENH_HEIGHT, SCREEN_WIDTH, 250);
    }];
}


- (IBAction)OK:(UIButton *)sender
{
    if (self.RegionType == HNPickViewProvice && _ProviceIndex == 0)
    {

        Provice *Model = _Model.Provices[_ProviceIndex];
        [_Model GetCityWithProvinceid:[Model.provinceid integerValue]];
    }
    else if (self.RegionType == HNPickViewCity && _CityIndex == 0)
    {
        City *Model = _Model.Cities[_CityIndex];
        [_Model GetAreaWithCityid:[Model.cityid integerValue]];
    }

    
    if (self.RegionType == HNPickViewProvice)
    {
        self.PickViewBlock (_RegionType,_ProviceIndex);
    }
    else if (self.RegionType == HNPickViewCity)
    {
        self.PickViewBlock (_RegionType,_CityIndex);
    }
    else if (self.RegionType == HNPickViewArea)
    {
        self.PickViewBlock (_RegionType,_AreaIndex);
    }
    else
    {
        self.PickViewBlock (_RegionType,_LeveIndex);
    }
    [self Close];
}
- (IBAction)Cancle:(UIButton *)sender
{
    [self Close];
}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (self.RegionType == HNPickViewProvice)
    {
        return _Model.Provices.count;
    }
    else if (self.RegionType == HNPickViewCity)
    {
        return _Model.Cities.count;
    }
    else if (self.RegionType == HNPickViewArea)
    {
        return _Model.Areas.count;
    }
    else
    {
        return [self.Leves count];
    }
}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = [UILabel new];
    label.textAlignment = NSTextAlignmentCenter;
    if (self.RegionType == HNPickViewProvice)
    {
        label.text = [_Model.Provices[row] provincename];
    }
    else if (self.RegionType == HNPickViewCity)
    {
        City *C = _Model.Cities[row];
        label.text = C.cityname;
    }
    else if (self.RegionType == HNPickViewArea)
    {
        Area *A = _Model.Areas[row];
        label.text = A.areaname;
    }
    else
    {
        label.text = self.Leves[row];
    }
    return label;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (self.RegionType == HNPickViewProvice)
    {
        _ProviceIndex = row;
        Provice *Model = _Model.Provices[row];
        [_Model GetCityWithProvinceid:[Model.provinceid integerValue]];
    }
    else if (self.RegionType == HNPickViewCity)
    {
        _CityIndex = row;
        City *Model = _Model.Cities[row];
        [_Model GetAreaWithCityid:[Model.cityid integerValue]];
    }
    else if (self.RegionType == HNPickViewArea)
    {
        _AreaIndex = row;
    }
    else
    {
        _LeveIndex = row;
    }
}

- (NSArray *)Leves
{
    if (!_Leves)
    {
        _Leves = @[@"一年级",@"二年级",@"三年级",@"四年级",@"五年级",@"六年级"];
    }
    return _Leves;
}

@end
